package com.jyw.business.obstacles.domain.dto.wechat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.jyw.business.obstacles.constant.OrderCurrStateConstant;
import com.jyw.business.obstacles.constant.OrderDealTypeConstant;
import com.jyw.business.obstacles.constant.OrderSourceConstant;
import com.jyw.business.obstacles.domain.ObsOrderFault;
import com.jyw.business.obstacles.domain.ObsOrderInfo;
import com.jyw.business.obstacles.domain.ObsOrderSend;
import com.jyw.business.obstacles.domain.dto.web.BaseOrderAddDto;
import com.jyw.business.obstacles.domain.dto.web.OrderAddDto;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.utils.FileUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 沈峻
 * @ClassName WechatOrderAddDto
 * @Description 微信创建工单请求实体
 * @Date 2023/8/10 15:34
 **/
@ApiModel("微信创建工单请求实体")
@Data
public class WechatOrderAddDto extends BaseOrderAddDto {

    @ApiModelProperty("录音")
    private String recording;

    @ApiModelProperty("派单对象标识")
    private List<Long> sendObjId;

    @Override
    public ObsOrderInfo buildOrder(){
        ObsOrderInfo orderInfo = super.buildOrder();
        orderInfo.setRecording(getRecording());
        return orderInfo;
    }

    @Override
    public ObsOrderFault buildOrderFault(ObsOrderInfo orderInfo) {
        ObsOrderFault orderFault = super.buildOrderFault(orderInfo);
        orderFault.setSource(OrderSourceConstant.WECHAT);
        return orderFault;
    }

    public List<ObsOrderSend> buildSend() {

        if(getSendType().equals(1)){
            ObsOrderSend send = new ObsOrderSend();
            //派单
            send.setType(1);
            //创建时间
            send.setCreateTime(DateUtils.getNowDate());
            //由我处理
            send.setSendUser(SecurityUtils.getUserId());
            send.setSendTeam(getSendTeam());
            send.setSendType(1);
            send.setCreateUser(SecurityUtils.getUserId());
            return Lists.newArrayList(send);
        }else{
            return getSendObjId().stream().map(data -> {
                ObsOrderSend send = new ObsOrderSend();
                //派单
                send.setType(1);
                //创建时间
                send.setCreateTime(DateUtils.getNowDate());
                send.setSendType(getSendObjType());
                switch (getSendObjType()) {
                    case 1:
                        send.setSendUser(data);
                        send.setSendTeam(getSendTeam());
                        break;
                    case 2:
                        send.setSendTeam(data);
                        break;
                    case 3:
                        send.setSendDept(data);
                        break;
                }
                send.setCreateUser(SecurityUtils.getUserId());
                return send;
            }).collect(Collectors.toList());
        }
    }

    @Override
    public boolean canBuildSend() {
        return !ObjectUtils.isEmpty(getSendType())&&getSendType().equals(1)||!CollectionUtils.isEmpty(getSendObjId());
    }
}
